//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Diagnostics.Contracts;

namespace OpenLS.Spreadsheet.Extensibility
{
#if DEBUG
    [ContractClass(typeof(ParameterConverterAttributeContract))]
#endif
    public abstract class ParameterConverterAttribute : Attribute, IParameterConverter, IParameterConverterProvider
    {
        private readonly ReferenceOption _refMode = ReferenceOption.ArgumentOrReference;

        protected ParameterConverterAttribute()
        {
        }

        protected ParameterConverterAttribute(ReferenceOption refMode)
        {
            this._refMode = refMode;
        }

        internal ReferenceOption RefMode
        {
            get { return _refMode; }
        }

        #region IParameterConverter Members

        void IParameterConverter.Convert(ConversionData data)
        {
            Convert(data);
        }

        public virtual int Priority { get { return 0; } }

        #endregion

        #region IParameterConverterProvider Members

        public IParameterConverter GetConverter()
        {
            return this;
        }

        #endregion

        private bool TryGetValue(object value, out object result)
        {
            if (value is ICell && RefMode != ReferenceOption.ArgumentOnly)
            {
                result = ((ICell) value).Value;
                return true;
            }
            if (!(value is ICell) && RefMode != ReferenceOption.ReferenceOnly)
            {
                result = value;
                return true;
            }
            result = null;
            return false;
        }

        //\\internal abstract bool Convert(object value, Type targetType, ref bool ignore, ref Error error, out object result);
        internal  void Convert(ConversionData data)
        {
            switch (this.RefMode)
            {
                case ReferenceOption.ArgumentOnly:
                    if (data.Dereferenced)
                        return
                            ;
                    break;
                case ReferenceOption.ReferenceOnly:
                    if (!data.Dereferenced)
                        return;
                    break;
            }
            if (data.Dereferenced && this.RefMode == ReferenceOption.ArgumentOnly)
                return;

            ConvertImp(data);
            
        }

        internal abstract void ConvertImp(ConversionData data);
    }
#if DEBUG
    [ContractClassFor(typeof (ParameterConverterAttribute))]
    public class ParameterConverterAttributeContract: ParameterConverterAttribute
    {
        internal override void ConvertImp(ConversionData data)
        {
            Contract.Requires(!data.Dereferenced || this.RefMode != ReferenceOption.ArgumentOnly);
            Contract.Requires(data.Dereferenced || this.RefMode != ReferenceOption.ReferenceOnly);
        }
    }
#endif
}